package com.itwzy.bookshop.controller;

import com.itwzy.bookshop.Bean.AdminsBean;
import com.itwzy.bookshop.Bean.ResultBean;
import com.itwzy.bookshop.DAO.AdminDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class AdminController {
    @Autowired
    private AdminDao adminDao;

    /**
     * 登录
     * @param account
     * @param pass
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public ResultBean login(String account, String pass,HttpSession session){
        ResultBean result = new ResultBean();
        AdminsBean admin = adminDao.selectByAccountAndPass(account,pass);
        if(admin == null){
            result.setCode(-1);
            result.setMsg("账号或密码错误!");
        }else {
            result.setCode(1);
            result.setMsg("登录成功!");
            result.setData(admin);

            session.setAttribute("admin",admin);
        }
        return result;
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public ResultBean logout(HttpSession session){
        ResultBean result = new ResultBean();
        session.invalidate();
        result.setCode(1);
        result.setMsg("退出成功");

        return result;
    }

    /**
     * 实现修改密码
     * @param oldPass
     * @param newPass
     * @param confirmPass
     * @param session
     * @return
     */
    @RequestMapping("/modify_pass")
    public ResultBean modifyPass(String oldPass,String newPass,String confirmPass,HttpSession session){
        ResultBean result = new ResultBean();
        //获得登录用户
        AdminsBean admin = (AdminsBean) session.getAttribute("admin");
        System.out.println(admin);
        //判断旧密码是否正确
        AdminsBean adminsBean = adminDao.selectByAccountAndPass(admin.getAccount(),oldPass);
        if(admin==null){
            result.setCode(-1);
            result.setMsg("原始密码不正确!");
            return result;
        }

        if (adminDao.updatePass(admin.getAccount(),newPass) == 1){
            result.setCode(1);
            result.setMsg("密码修改成功");
            return result;
        }else {
            result.setCode(-1);
            result.setMsg("密码修改失败");
        }
        return result;
    }

    /**
     * 修改管理员
     * @param admins
     * @return
     */
    @RequestMapping("/edit_admin")
    public ResultBean editAdmin(AdminsBean admins){
        ResultBean result = new ResultBean();
        if (adminDao.updateAdmin(admins)==1){
            result.setCode(1);
            result.setMsg("个人信息修改成功");
        }else {
            result.setCode(-1);
            result.setMsg("个人信息修改成功");
        }
        return result;
    }

    /**
     * 根据id查询管理员
     * @param id
     * @return
     */
    @RequestMapping("/get_admin_byid")
    public ResultBean getAdminByid(String id){
        ResultBean result = new ResultBean();
        AdminsBean admin = adminDao.selectById(id);
        if(admin == null){
            result.setCode(-1);
            result.setMsg("管理员不存在!");
        }else {
            result.setCode(1);
            result.setData(admin);
        }
        return result;
    }

}
